package fast.loader;

import fast.data.Stat;
import fast.data.StatBlock;

public class ItemEquipped {
	
	private Item item;
	private int itemSuffix;
	private Reforge reforge = null;
	public Gem[] gem = new Gem[3];
	private Enchant enchant = null;

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}
	public void removeReforge(){
		this.reforge = null;
	}
	public Reforge getReforge() {
		return reforge;
	}

	public void setReforge(Reforge reforge) {
		this.reforge = reforge;
	}

	public Enchant getEnchant() {
		return enchant;
	}

	public void setEnchant(Enchant enchant) {
		this.enchant = enchant;
	}
		
	public Gem getGem(int slot){
		return gem[slot];
	}
	
	public void setGem(int slot, Gem gem){
		if(this.gem[slot] != null){
			if(this.gem[slot].equalStats(gem))
				return;
		}
		this.gem[slot] = gem;
	}
	
	public StatBlock getReforgeAsBlock(){
		if(reforge == null)
			return new StatBlock();
		int value = getReforgeValue();
		StatBlock stats = new StatBlock();
		stats.setStat(reforge.getFrom(), -value);
		stats.setStat(reforge.getTo(), value);
		return stats;
	}
	
	public int getReforgeValue(){
		int value = 0;
		if(reforge != null){
			value = (int) Math.floor(item.getStat(reforge.getFrom())*0.4);
		}
		return value;
	}
	
	public int getReforgedStat(Stat stat) {
		int value = 0;
		if(hasItemSuffix()){
			value += getItemSuffixStatValue(stat.getReforgeName());
			//System.out.println("Random suffix " + stat.getName() +  " " + value);
		}
		if(reforge != null){
			if(reforge.getFrom() == stat){
				value = getReforgeValue();
			}
			else if(reforge.getTo() == stat){
				value -= getReforgeValue();
			}
		}
		return (int) (item.getStat(stat)-value);
	}
	
	public boolean gemsMatch(){
		for(int i = 0; i < item.gemSlot.length; i++){
			Socket gemSlot = item.gemSlot[i];
			if(gemSlot == null) continue;
			Gem elem = gem[i];
			boolean valid_gem = false;
			if(elem == null && gemSlot != Socket.prismatic){
				return false;
			}
			if(gemSlot == Socket.meta){
				if(elem.getType() == Gem.META)
					valid_gem = true;
			}
			else if(gemSlot == Socket.red){
				if(elem.getType() == Gem.RED || elem.getType() == Gem.ORANGE || elem.getType() == Gem.PURPLE)
					valid_gem = true;
			}
			else if(gemSlot == Socket.yellow){
				if(elem.getType() == Gem.YELLOW || elem.getType() == Gem.ORANGE || elem.getType() == Gem.GREEN)
					valid_gem = true;
			}
			else if(gemSlot == Socket.blue){
				if(elem.getType() == Gem.BLUE || elem.getType() == Gem.GREEN || elem.getType() == Gem.PURPLE)
					valid_gem = true;
			}
			else if(gemSlot == Socket.prismatic){
				if(elem != null && elem.getType() != Gem.COGWHEEL && elem.getType() != Gem.HYDRAULIC && elem.getType() != Gem.META)
					valid_gem = true;
			}
			/*else if(gemSlot == Socket.cogwheel)){
				if(elem.getType() == Gem.COGWHEEL)
					valid_gem = true;
			}*/
			else if(gemSlot == Socket.hydraulic){
				if(elem.getType() == Gem.HYDRAULIC)
					valid_gem = true;
			}
			if(!valid_gem)
				return false;
		}
		return true;
	}
	
	private boolean hasItemSuffix(){
		if(itemSuffix > 0) return true;
		return false;
	}

	public int getItemSuffix() {
		return itemSuffix;
	}

	public void setItemSuffix(int itemSuffix) {
		this.itemSuffix = itemSuffix;
		
		
		System.out.println(itemSuffix);
		ItemSuffixDB isDB = new ItemSuffixDB();
		int count = 5; //isDB.GetStatCount(itemSuffix);
		//System.out.println("Num Stats: " + count);
		for(int i = 0;i < count; i++){
			if(isDB.GetStat(itemSuffix, i) == null) continue;
			//System.out.println("Stat " + isDB.GetStat(itemSuffix, i));
			//System.out.println("Stat Value " + isDB.GetStatValue(item, itemSuffix, i));
			Stat stat = isDB.GetStat(itemSuffix, i);
			int value = (int) Math.floor(isDB.GetStatValue(item, itemSuffix, i));
			item.setStat(stat, value);
		}
	}
	
	private int getItemSuffixStatValue(String stat){
		ItemSuffixDB isDB = new ItemSuffixDB();
		//System.out.println(itemSuffix);
		int count = 5; //isDB.GetStatCount(itemSuffix);
		//System.out.println("number of suffixes: " + count);
		for(int i = 0;i < count; i++){
			if(isDB.GetStat(itemSuffix, i) == null) continue;
			//System.out.println(isDB.GetStat(itemSuffix, i));
			if(isDB.GetStat(itemSuffix, i).equals(stat)){
				return (int) isDB.GetStatValue(item, itemSuffix, i);
			}
		}
		return 0;
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("<b>"+item.getName()+"</b><br>");
		if(item.getTag().length() > 0)
			sb.append(item.getTag()+"<br>");
		sb.append("Item Level: " + item.getItemLevel()+"<br>");
		if(item.isUpgradable())
			sb.append("Upgrade Level: " + item.getUpgradeLevel()+"/"+item.getMaxUpgradeLevel()+"<br>");
		if(item.getDps() != 0.0){
			sb.append(item.getDps() + " DPS ");
			sb.append(item.getSpeed() + " Speed<br>");
		}
		for(Stat stat : Stat.possibleStats){
			int value = getReforgedStat(stat);
			if(value != 0){
				sb.append("+" + value + " " + stat.getName());
				if(reforge != null && reforge.getTo() == stat)
					sb.append(" (from " + reforge.getFrom().getName() + ")");
				sb.append("<br>");
			}
		}
		if(enchant != null)
			sb.append("Enchant: " + enchant.getName()+"<br>");
		for(Socket gemSlot : item.getGemSlot()){
			if(gemSlot == null) continue;
			sb.append(gemSlot + " Socket<br>");
		}
		if(item.getSocketBonus() != null)
			sb.append(item.getSocketBonus().toString()+"<br>");
		if(item.getSet() != null && !item.getSet().equals(""))
			sb.append("Set: " + item.getSet()+"<br>");
		if(item.getGearBuff() != null)
			sb.append("GearBuff: " + item.getGearBuff()+"<br>");
		return sb.toString();
	}
}
